/*
This codefile contains the functionality of the "HardwareOut" 
component, specified in the design report ("ontwerprapport.doc").

Author:			Daan
Version: 		1
Last modified:	14-05-11
*/

// handler functions for state changes on the pins
void HandleCallToFloor3 ()
{
	ElevatorLogic_GoTo(3);
}
void HandleCallToFloor2 ()
{
	ElevatorLogic_GoTo(2);
}
void HandleCallToFloor1 ()
{
	ElevatorLogic_GoTo(1);
}
void HandleCallToFloor0 ()
{
	ElevatorLogic_GoTo(0);
}
void HandleCabinAtFloor3 ()
{
	ElevatorLogic_At(3);
}
void HandleCabinBelowFloor3 ()
{
	ElevatorLogic_Below(3);
}
void HandleCabinAboveFloor2 ()
{
	ElevatorLogic_Above(2);
}
void HandleCabinAtFloor2 ()
{
	ElevatorLogic_At(2);
}
void HandleCabinBelowFloor2 ()
{
	ElevatorLogic_Below(2);
}
void HandleCabinAboveFloor1 ()
{
	ElevatorLogic_Above(1);
}
void HandleCabinAtFloor1 ()
{
	ElevatorLogic_At(1);
}
void HandleCabinBelowFloor1 ()
{
	ElevatorLogic_Below(1);
}
void HandleCabinAboveFloor0 ()
{
	ElevatorLogic_Above(0);
}
void HandleCabinAtFloor0 ()
{
	ElevatorLogic_At(0);
}
void HandleOverheating ()
{
	ElevatorLogic_CallEmergency();
}
void HandleTemeratureChange ()
{
	// ?
}
void HandleEmergencyCall ()
{
	ElevatorLogic_CallEmergency();
}


BOOL previousValue[21];

// initializing function for this component
void HardwareOut_Initialize ()
{
	int i;
	for (i=0; i<21; i++)
	{
		previousValue[i] = OFF;
	}
}

void HardwareOut_CheckOutput ()
{
	if (previousValue[0] == OFF && PDR0_P00 == ON)
	{
		HandleCallToFloor0();
	}
	if (previousValue[1] == OFF && PDR0_P01 == ON)
	{
		HandleCallToFloor1();
	}
	if (previousValue[2] == OFF && PDR0_P02 == ON)
	{
		HandleCallToFloor2();
	}
	if (previousValue[3] == OFF && PDR0_P03 == ON)
	{
		HandleCallToFloor3();
	}
	if (previousValue[4] == OFF && PDR2_P25 == ON)
	{
		HandleCallToFloor0();
	}
	if (previousValue[5] == OFF && PDR2_P24 == ON)
	{
		HandleCallToFloor1();
	}
	if (previousValue[6] == OFF && PDR2_P21 == ON)
	{
		HandleCallToFloor2();
	}
	if (previousValue[7] == OFF && PDR2_P20 == ON)
	{
		HandleCallToFloor3();
	}
	if (previousValue[8] == OFF && PDR5_P57 == ON)
	{
		HandleCabinAtFloor3();
	}
	if (previousValue[9] == OFF && PDR5_P56 == ON)
	{
		HandleCabinBelowFloor3();
	}
	if (previousValue[10] == OFF && PDR5_P55 == ON)
	{
		HandleCabinAboveFloor2();
	}
	if (previousValue[11] == OFF && PDR5_P54 == ON)
	{
		HandleCabinAtFloor2();
	}
	if (previousValue[12] == OFF && PDR5_P53 == ON)
	{
		HandleCabinBelowFloor2();
	}
	if (previousValue[13] == OFF && PDR5_P52 == ON)
	{
		HandleCabinAboveFloor1();
	}
	if (previousValue[14] == OFF && PDR5_P51 == ON)
	{
		HandleCabinAtFloor1();
	}
	if (previousValue[15] == OFF && PDR5_P50 == ON)
	{
		HandleCabinBelowFloor1();
	}
	if (previousValue[16] == OFF && PDR0_P05 == ON)
	{
		HandleCabinAboveFloor0();
	}
	if (previousValue[17] == OFF && PDR0_P04 == ON)
	{
		HandleCabinAtFloor0();
	}
	if (previousValue[18] == OFF && PDR0_P06 == ON)
	{
		HandleOverheating();
	}
	if (previousValue[19] == OFF && PDR0_P07 == ON)
	{
		HandleTemeratureChange();
	}
	if (previousValue[20] == OFF && PDR1_P17 == ON)
	{
		beep(5);
		HandleEmergencyCall();
	}
	
	previousValue[0] = (BOOL) PDR0_P00;
	previousValue[1] = (BOOL) PDR0_P01;
	previousValue[2] = (BOOL) PDR0_P02;
	previousValue[3] = (BOOL) PDR0_P03;
	previousValue[4] = (BOOL) PDR2_P25;
	previousValue[5] = (BOOL) PDR2_P24;
	previousValue[6] = (BOOL) PDR2_P21;
	previousValue[7] = (BOOL) PDR2_P20;
	previousValue[8] = (BOOL) PDR5_P57;
	previousValue[9] = (BOOL) PDR5_P56;
	previousValue[10] = (BOOL) PDR5_P55;
	previousValue[11] = (BOOL) PDR5_P54;
	previousValue[12] = (BOOL) PDR5_P53;
	previousValue[13] = (BOOL) PDR5_P52;
	previousValue[14] = (BOOL) PDR5_P51;
	previousValue[15] = (BOOL) PDR5_P50;
	previousValue[16] = (BOOL) PDR0_P05;
	previousValue[17] = (BOOL) PDR0_P04;
	previousValue[18] = (BOOL) PDR0_P06;
	previousValue[19] = (BOOL) PDR0_P07;
	previousValue[20] = (BOOL) PDR1_P17;
}